/*
 * @Author: xiaosihan
 * @Date: 2024-12-15 15:57:19
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-12-15 16:04:56
 */

import { BackSide, FrontSide, Group, MeshStandardMaterial } from "three";
import MeshBase from "three-base/MeshBase";
import threeUtils from "three-base/threeUtils";

// 衣服模型
export default class ClothMesh extends Group {
  constructor(url?: string) {
    super();
    if (url) {
      this.load(url);
    }
  }

  load(url?: string) {
    this.front_cloth.loadModel(url);
    this.back_cloth.loadModel(url);
  }

  // 衣服模型(正面)
  front_cloth = (() => {
    const front_cloth = new MeshBase();
    front_cloth.addEventListener("loaded", () => {
      threeUtils.scaleTo(front_cloth, 5, ["x", "y"]);
      threeUtils.center(front_cloth);
      front_cloth.traverseMesh((mesh) => {
        mesh.material = (mesh.material as MeshStandardMaterial).clone();
        (mesh.material as MeshStandardMaterial).color.set("#aaffaa");
        (mesh.material as MeshStandardMaterial).metalness = 0;
        (mesh.material as MeshStandardMaterial).roughness = 0.8;
        (mesh.material as MeshStandardMaterial).side = FrontSide;
      });
      this.add(front_cloth);
    });
    return front_cloth;
  })();

  // 衣服模型(反面)
  back_cloth = (() => {
    const back_cloth = new MeshBase();
    back_cloth.addEventListener("loaded", () => {
      threeUtils.scaleTo(back_cloth, 5, ["x", "y"]);
      threeUtils.center(back_cloth);
      back_cloth.traverseMesh((mesh) => {
        mesh.material = (mesh.material as MeshStandardMaterial).clone();
        (mesh.material as MeshStandardMaterial).color.set("#ffffff");
        (mesh.material as MeshStandardMaterial).metalness = 0;
        (mesh.material as MeshStandardMaterial).roughness = 0.8;
        (mesh.material as MeshStandardMaterial).side = BackSide;
      });
      this.add(back_cloth);
    });
    return back_cloth;
  })();
}
